﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using X360;
using X360.STFS;

         //NOTE: The X360 Library is protected under a specific Terms and Service and License
        
         //Make sure you read that before doing anything, mainly because it REQUIRES you
        
         //To make everything open source and NOT make profit off it


namespace CONSaver_2
{
    class X360code
    {

        string pathToKV = "KV.bin";

        internal bool ResignPackage(string pathToFile)
        {
            bool returnValue = false;
            STFSPackage xPackage = null;

            try
            {
                xPackage = new STFSPackage(pathToFile, null);

                if (xPackage.ParseSuccess)
                {
                    if (File.Exists(pathToKV))
                    {
                        if (xPackage.FlushPackage(new RSAParams(pathToKV)))
                        {
                            returnValue = true;
                        }
                        else
                        {
                            returnValue = false;
                            //Add to log
                        }
                    }
                }
            }
            catch
            {
                returnValue = false;
            }
            finally
            {
                if(xPackage != null)
                    xPackage.CloseIO();
            }
            return returnValue;
        }

        internal string ExtractProfileName(string Path)
        {
            string Value = null;

                STFSPackage xPackage = new STFSPackage(Path, null);
                ProfilePackage pPackage = new ProfilePackage(ref xPackage);

                if (pPackage.Header.ThisType == PackageType.Profile)
                {
                    Value = pPackage.UserFile.GetGamertag().ToString();
                    pPackage.CloseIO();
                }
                else
                {
                    pPackage.CloseIO();

                }

            return Value;
        }

        internal string ExtractProfileID(string path)
        {
            string Value = null;

            STFSPackage xPackage = null;

            try
            {
                xPackage = new STFSPackage(path, null);

                if (xPackage.ParseSuccess)
                {

                        Value = String.Format("{0:X}", xPackage.Header.ProfileID);

                }
            }
            catch
            {
                Value = null;
            }
            finally
            {
                if (xPackage != null)
                    xPackage.CloseIO();
            }



            return Value;
        }

    }
}
